package com.example.gkcircle.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.example.gkcircle.R;
import com.example.gkcircle.activity.ComposeLostActivity;
import com.example.gkcircle.activity.PhotoViewAcitivty;
import com.example.gkcircle.bean.Lost;
import com.example.gkcircle.bean.Wish;
import com.example.gkcircle.fragment.LoveFragment;
import com.example.gkcircle.fragment.lost.FoundFragment;
import com.example.gkcircle.fragment.lost.LostFragment;
import com.example.gkcircle.fragment.lost.search.SearchFoundFragment;
import com.example.gkcircle.fragment.lost.search.SearchLostFragment;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.MyAlertView;
import com.example.gkcircle.utils.PicUtil;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.utils.UniversalImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 失物
 * Created by Jehu on 16/3/19.
 */
public class LostAdapter extends BaseAdapter{


    private List<Lost> mData;
    private Activity mActivity;
    private LayoutInflater inflater;
    private int mFlag;
    public static final int LOST_FLAG=0;
    public static final int FOUND_FLAG=1;
    public static final int SEARCH_LOST_FLAG=2;
    public static final int SEARCH_FOUND_FLAG=3;

    public LostAdapter(Activity activity,List<Lost> data,int flag) {
        this.mData=data;
        this.mFlag=flag;
        this.mActivity = activity;
        this.inflater = LayoutInflater.from(mActivity);
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class LostViewHolder{
        ImageView avatar;
        TextView username;
        TextView date;
        TextView content;
        LinearLayout watch;
        ImageButton phone;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LostViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new LostViewHolder();
            convertView=inflater.inflate(R.layout.home_lost_list_item,null);
            viewHolder.avatar=(ImageView)convertView.findViewById(R.id.home_lost_item_avatar);
            viewHolder.username=(TextView)convertView.findViewById(R.id.home_lost_item_username);
            viewHolder.date=(TextView)convertView.findViewById(R.id.home_lost_item_date);
            viewHolder.content=(TextView)convertView.findViewById(R.id.home_lost_item_content);
            viewHolder.watch=(LinearLayout)convertView.findViewById(R.id.home_lost_item_watch);
            viewHolder.phone=(ImageButton)convertView.findViewById(R.id.home_lost_item_phone);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(LostViewHolder)convertView.getTag();
        }

        final Lost lost=mData.get(position);
        final AVUser user=lost.getUser();

        viewHolder.username.setText(user.get("nickname").toString());
        viewHolder.content.setText(lost.getContent());
        viewHolder.date.setText(lost.getCreatedAt());

        //用户头像
        if(null!=user.getAVFile("avatar")){
            int width=mActivity.getResources().getDimensionPixelSize(R.dimen.avatar_width);
            //获取缩略图url
            String imgUrl=user.getAVFile("avatar").getThumbnailUrl(true,width,width);
            UniversalImageLoader.getIntance().displayImage(imgUrl,viewHolder.avatar,UniversalImageLoader.getAvatarOptions(),UniversalImageLoader.getAnimateFirstDisplayListener());
        }else{
            viewHolder.avatar.setImageResource(R.mipmap.personal_default_user_icon);
        }

        //拨打电话
        viewHolder.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber=lost.getContacts();
                //回调显示AlertView
                switch (mFlag){
                    case LOST_FLAG:
                        LostFragment.onShowAlertViewListener.show(phoneNumber);
                        break;
                    case FOUND_FLAG:
                        FoundFragment.onShowAlertViewListener.show(phoneNumber);
                        break;
                    case SEARCH_LOST_FLAG:
                        SearchLostFragment.onShowAlertViewListener.show(phoneNumber);
                        break;
                    case SEARCH_FOUND_FLAG:
                        SearchFoundFragment.onShowAlertViewListener.show(phoneNumber);
                        break;
                }
            }
        });

        if(null!=lost.getPhotoUrl()){
            viewHolder.watch.setVisibility(View.VISIBLE);
            //查看图片
            viewHolder.watch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = lost.getPhotoUrl();
                    Intent intent=new Intent(mActivity,PhotoViewAcitivty.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("imageUrl", url);
                    intent.putExtras(bundle);
                    mActivity.overridePendingTransition(0, R.anim.zoom_in);
                    mActivity.startActivity(intent);
                }
            });
        }else{
            viewHolder.watch.setVisibility(View.GONE);
        }

        //点击头像
        viewHolder.avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }

}
